﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Niti.OrderManage.EntityFrameworkCore.DbMigrations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Niti.Blog.EntityFrameworkCore.DbMigrations
{
    public class NitiOrderManageMigrationsDbContextFactory : IDesignTimeDbContextFactory<NitiOrderManageMigrationsDbContext>
    {
        public NitiOrderManageMigrationsDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<NitiOrderManageMigrationsDbContext>()
            .UseMySql(configuration.GetConnectionString("Default"),new MySqlServerVersion(new Version(8, 0, 11)));
            // .UseSqlServer(configuration.GetConnectionString("SqlServer"));

            return new NitiOrderManageMigrationsDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            return builder.Build();
        }
    }
}
